package cnd;

/**
 * The DownloadController controls all downloads
 * @author Marten Jacobs
 * @version 0.1
 */
class DownloadController {

    Controller controller;
    Download[] downloads = null;
    /**
     * @param controller
     * The controller with which to communicate
     */
    public DownloadController(Controller controller) {
        this.controller = controller;
    }
    /**
     * Add a download and start it
     * @param searchResult
     * The SearchResult to use when starting the Download
     */
    public void addDownload(SearchResult searchResult) {
        java.awt.FileDialog fileDialog = new java.awt.FileDialog(new java.awt.Frame(),
                "Specify location to save \"" + searchResult.name + "\"", java.awt.FileDialog.SAVE);
        fileDialog.setFile(searchResult.name);
        fileDialog.setVisible(true);
        if (fileDialog.getFile() != null) {
            String target = fileDialog.getDirectory() + fileDialog.getFile();
            if (target != null) {
                Download dl = new Download(searchResult, target);
                int i;
                if (downloads == null) {
                    downloads = new Download[1];
                    i = 0;
                } else {
                    Download[] temp_downloads = downloads;
                    downloads = new Download[temp_downloads.length + 1];
                    System.arraycopy(temp_downloads, 0, downloads, 0, temp_downloads.length);
                    i = temp_downloads.length;
                }
                downloads[i] = dl;
                downloads[i].start();
            }
        }
    }
}
